﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace Common.Tool
{
    public class ColorPickerHandle
    {
        /**
           * \brief 文本颜色转换至RGB色彩参数 2019.09.18 wj 增
           * \param[in] textColor 文本颜色RGB字符串格式
           * \retval SolidColorBrush 单色画笔参数
           */
        public static Color TextColorToColor(string textColor)
        {
            Color color = Color.FromArgb(255, 255, 255, 255);
            try
            {
                string colorString = String.Empty;
                // 补充透明度参数，并默认其为255
                if (textColor.StartsWith("#"))
                {
                    colorString = String.Format("FF{0}", textColor.Substring(1, 6));
                }
                else
                {
                    colorString = String.Format("FF{0}", textColor.Substring(0, 6));
                }
                Int32 colorValue = Convert.ToInt32(colorString, 16);
                color = System.Drawing.Color.FromArgb(colorValue);
            }
            catch
            { }
            return color;
        }

        /**
           * \brief 单色画笔参数转换至文本颜色 2019.09.18 wj 增
           * \param[in] brush 单色画笔参数
           * \retval textColor 文本颜色RGB字符串格式
           */
        public static string ColorToTextColor(Color color)
        {
            Int32 value = Convert.ToInt32(color.ToArgb());
            string colorString = String.Format("{0:X}", value);
            string textColor = String.Format("#{0}", colorString.Substring(2, 6));
            return textColor;
        }

        /**
           * \brief 文本颜色转换至单色画笔参数 2019.09.18 wj  增
           * \param[in] textColor 文本颜色RGB字符串格式
           * \retval SolidColorBrush 单色画笔参数
           */
        public static System.Windows.Media.SolidColorBrush TextColorToBrush(string textColor)
        {
            // 默认为白色
            System.Windows.Media.Color color = System.Windows.Media.Color.FromArgb(255, 255, 255, 255);
            System.Windows.Media.SolidColorBrush brush = new System.Windows.Media.SolidColorBrush(color);
            try
            {
                Int32 colorRedVaule = Convert.ToInt32(textColor.Substring(1, 2), 16);
                Int32 colorGreenVaule = Convert.ToInt32(textColor.Substring(3, 2), 16);
                Int32 colorBlueVaule = Convert.ToInt32(textColor.Substring(5, 2), 16);
                color = System.Windows.Media.Color.FromArgb(255, (byte)colorRedVaule, (byte)colorGreenVaule, (byte)colorBlueVaule);
                brush = new System.Windows.Media.SolidColorBrush(color);
                return brush;
            }
            catch
            { }
            return brush;
        }

        /**
           * \brief 文本颜色转换至RGB参数 2019.09.18 wj  增
           * \param[in] textColor 文本颜色RGB字符串格式
           * \retval RGBValue RGB色彩数组
           */
        public static string[] TextColorToRGB(string textColor)
        {
            // 默认为白色
            string[] RGBValue = new string[3] { "255", "255", "255" };
            try
            {
                Int32 colorRedVaule = Convert.ToInt32(textColor.Substring(1, 2), 16);
                Int32 colorGreenVaule = Convert.ToInt32(textColor.Substring(3, 2), 16);
                Int32 colorBlueVaule = Convert.ToInt32(textColor.Substring(5, 2), 16);
                RGBValue[0] = colorRedVaule.ToString();
                RGBValue[1] = colorGreenVaule.ToString();
                RGBValue[2] = colorBlueVaule.ToString();
            }
            catch
            { }
            return RGBValue;
        }

        /**
          * \brief RGB参数转换至文本颜色 2019.09.18 wj 增
          * \param[in] RGBValue  RGB色彩数组
          * \retval textColor 文本颜色RGB字符串格式
          */
        public static string RGBToTextColor(string[] RGBValue)
        {
            // 默认为白色
            string textColor = "#FFFFFF";
            try
            {
                Int32 colorRedVaule = Convert.ToInt32(RGBValue[0]);
                Int32 colorGreenVaule = Convert.ToInt32(RGBValue[1]);
                Int32 colorBlueVaule = Convert.ToInt32(RGBValue[2]);
                System.Windows.Media.Color color = System.Windows.Media.Color.FromArgb(255, (byte)colorRedVaule, (byte)colorGreenVaule, (byte)colorBlueVaule);
                textColor = color.ToString().Remove(1, 2);
            }
            catch
            { }
            return textColor;
        }

        /**
         * \brief 字体颜色参数变更 2021-05-07 wj 增
         * \param[in] initColor 初始颜色值
         * \retval res 最终颜色值
         */
        public static string ShowColorDialog(string initColor)
        {
            ColorDialog colorDialog = new ColorDialog();
            //允许使用该对话框的自定义颜色
            colorDialog.AllowFullOpen = true;
            colorDialog.FullOpen = true;
            colorDialog.AnyColor = true;
            //初始化当前文本框中的字体颜色
            colorDialog.Color = ColorPickerHandle.TextColorToColor(initColor);
            //当用户在ColorDialog对话框中点击"取消"按钮
            colorDialog.ShowDialog();
            // 为文本颜色参数及相关控件赋值
            return  ColorPickerHandle.ColorToTextColor(colorDialog.Color);
        }
    }
}
